package com.system.xingzhi.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.xingzhi.dao.mapper.PageMapper;
import com.system.xingzhi.dao.mapper.RoleMapper;
import com.system.xingzhi.model.Page;
import com.system.xingzhi.model.ReturnData;
import com.system.xingzhi.model.Role;
import com.system.xingzhi.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    PageMapper pageMapper;

    @Override
    public ReturnData deleteByPrimaryKey(String pageId) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(pageMapper.deleteByPrimaryKey(pageId));
            returnData.setCode(0);
            returnData.setDetail("删除页面成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("删除页面 失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData insert(Page record) {
        ReturnData returnData=new ReturnData();
        try {
            returnData.setData(pageMapper.insert(record));
            returnData.setCode(0);
            returnData.setDetail("添加页面成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("添加页面失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public int insertSelective(Page record) {
        return pageMapper.insertSelective(record);
    }

    @Override
    public Page selectByPrimaryKey(String pageId) {
        return pageMapper.selectByPrimaryKey(pageId);
    }

    @Override
    public int updateByPrimaryKeySelective(Page record) {
        return pageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ReturnData updateByPrimaryKey(Page record) {
        ReturnData returnData=new ReturnData();
        try {
            if (record.getComponentId() == "") {
                record.setComponentId(null);
            }
            returnData.setData(pageMapper.updateByPrimaryKeySelective(record));
            returnData.setCode(0);
            returnData.setDetail("更新页面成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("更新页面失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData selectByParameter(Map<String, String> map,Integer pageNum, Integer pageSize) {
        ReturnData returnData=new ReturnData();
        try {
            //分页
            PageHelper.startPage(pageNum, pageSize);

            List<Page> queryResult = pageMapper.selectByParameter(map);
            returnData.setData(new PageInfo<>(queryResult));
            returnData.setCode(0);
            returnData.setDetail("查询页面数据成功");
        }catch (Exception e){
            returnData.setCode(1);
            returnData.setDetail("查询页面失败");
            return returnData;
        }
        return returnData;
    }

    @Override
    public ReturnData getAllPage() {
        ReturnData returnData=new ReturnData();
        try {
            List<Page> queryResult = pageMapper.getAllPage();
            returnData.setData(new PageInfo<>(queryResult));
            returnData.setCode(0);
            returnData.setDetail("获取所有页面成功");
        }catch (Exception e){
            e.printStackTrace();
            returnData.setCode(1);
            returnData.setDetail("获取所有页面失败");
            return returnData;
        }
        return returnData;
    }
}
